//
// Created by 紫霄 on 2020/4/12.
//

#ifndef LEETCODE_16_H
#define LEETCODE_16_H
class Solution {
public:
  double myPow(double x, int n) {
    double ans = x;
    if (x == 1) {
      return 1;
    } else {
      if (n == 0) {
        return 1;
      }
      if (n < 0) {
        if (n < -2147483647) {
          int i = -2147483647;
          int j = n - i;
          return myPow(x, i) * myPow(x, j);

        } else {
          return (1 / myPow(x, -n));
        }
      } else {
        if (n == 1) {
          return x;
        }
        if (n % 2) {
          auto k = myPow(x, (n - 1) / 2);
          return x * k * k;
        } else {
          auto k = myPow(x, n / 2);
          return k * k;
        }
      }
      return ans;
    }
  }
};
#endif // LEETCODE_16_H
